<?php
namespace Library;
class Qimendunjia{
    public function index($birthday,$remarks){
        $remarksArr=json_decode($remarks,true);
        $qijuType=$remarksArr['qijuType'];//0公历起局   1四柱起局
        $sizhu=$remarksArr['sizhu'];
        $jushu=$remarksArr['jushu'];
        $qimen=$remarksArr['qimen'];//0转盘奇门    1飞盘奇门
        $runfa=$remarksArr['runfa'];//0超接置闰法  1拆补无闰法

        $tiangan=array('0','甲','乙','丙','丁','戊','己','庚','辛','壬','癸');//天干
        $dizhi=array('0','子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥');//地支

        if($qijuType==1){
            $sGz=$this->shiGzGet($sizhu,$tiangan,$dizhi);
            $sizhu[3]=$sGz;
            $Arr['sizhu']=$sizhu;
        }else{
            $ymdArr=explode('-',$birthday);
            $y=$ymdArr[1];$m=$ymdArr[2];$d=$ymdArr[3];$hour=$ymdArr[4];
            $timeName="{$y}年{$m}月{$d}日{$hour}时";
            $Arr['time']=$timeName;
            $zymd=$y.'-'.str_pad($m,2,"0",STR_PAD_LEFT).'-'.str_pad($d,2,"0",STR_PAD_LEFT);
            $zyinli=yinli($y,$m,$d);
            $jieqi=$zyinli['jieqi']['nameQmdj'];
            jieqiapi($y,$jieqi);
            $jieqi24=cookie('24jieqi');
            $jqYmd=mb_substr($jieqi24,0,4,'utf-8').'-'.mb_substr($jieqi24,5,2,'utf-8').'-'.mb_substr($jieqi24,8,2,'utf-8');
            $Arr['jieqi']="{$jieqi}:{$jieqi24}";
            $jqDay=mb_substr($jieqi24,8,2,'utf-8');
            if($zymd!=''){
                zwmp($zymd,$hour,'男','sizhu');
                $sizhu=I('globals.sizhu');//Z四柱
                $Arr['sizhu']=$sizhu;
                $riG=mb_substr($sizhu[2],0,1,'utf-8');
            }
        }


        $yonju24=array(//1阳遁  0阴遁
            "冬至"=>'1-174', "小寒"=>'1-285', "大寒"=>'1-396', "立春"=>'1-852', "雨水"=>'1-963', "惊蛰"=>'1-174', "春分"=>'1-396', "清明"=>'1-417', "谷雨"=>'1-528', "立夏"=>'1-417', "小满"=>'1-528', "芒种"=>'1-639',
            "夏至"=>'0-936', "小暑"=>'0-825', "大暑"=>'0-714', "立秋"=>'0-258', "处暑"=>'0-147', "白露"=>'0-936', "秋分"=>'0-714', "寒露"=>'0-693', "霜降"=>'0-582', "立冬"=>'0-693', "小雪"=>'0-582', "大雪"=>'0-471',
        );
        $numberName=array(1=>'一',2=>'二',3=>'三',4=>'四',5=>'五',6=>'六',7=>'七',8=>'八',9=>'九');
        $daXieNum=array('一'=>1,'二'=>2,'三'=>3,'四'=>4,'五'=>5,'六'=>6,'七'=>7,'八'=>8,'九'=>9);
        $xunshouArr=array(
            '戊'=>array('戌亥','甲子','乙丑','丙寅','丁卯','戊辰','己巳','庚午','辛未','壬申','癸酉'),
            '己'=>array('申酉','甲戌','乙亥','丙子','丁丑','戊寅','己卯','庚辰','辛巳','壬午','癸未'),
            '庚'=>array('午未','甲申','乙酉','丙戌','丁亥','戊子','己丑','庚寅','辛卯','壬辰','癸巳'),
            '辛'=>array('辰巳','甲午','乙未','丙申','丁酉','戊戌','己亥','庚子','辛丑','壬寅','癸卯'),
            '壬'=>array('寅卯','甲辰','乙巳','丙午','丁未','戊申','己酉','庚戌','辛亥','壬子','癸丑'),
            '癸'=>array('子丑','甲寅','乙卯','丙辰','丁巳','戊午','己未','庚申','辛酉','壬戌','癸亥')
        );

        if($qijuType==1){
        }else{
            $yinli=$zyinli[0].'年'.$zyinli[1].$zyinli[2].'日'.mb_substr($sizhu[3],1,1,'utf-8').'时';//阴历
            $Arr['yinli']=$yinli;
        }


        //旬首
        foreach($xunshouArr as $key=>$value){
            if(array_search($sizhu[3],$value)){$xsName=$key;break;}
        }
        $Arr['xsName']="{$sizhu[3]}时旬首为{$xsName}";

        //旬空
        if($qijuType==1){//四柱起局
        }else{
            foreach($sizhu as $key=>$value){
                foreach($xunshouArr as $k=>$v){
                    if(array_search($value,$v)){
                        $Arr['xunkong'][$key]=$v[0];
                    }
                }
            }
        }

        //定局数
        $yonju24Arr=array(
            "冬至","小寒","大寒","立春","雨水","惊蛰","春分","清明","谷雨","立夏","小满","芒种",
            "夏至","小暑","大暑","立秋","处暑","白露","秋分","寒露","霜降","立冬","小雪","大雪",
        );
        //冬至->己卯
        $lsjiazi=array('0','甲子','乙丑','丙寅','丁卯','戊辰','己巳','庚午','辛未','壬申','癸酉','甲戌','乙亥','丙子','丁丑','戊寅',
                           '己卯','庚辰','辛巳','壬午','癸未','甲申','乙酉','丙戌','丁亥','戊子','己丑','庚寅','辛卯','壬辰','癸巳',
                           '甲午','乙未','丙申','丁酉','戊戌','己亥','庚子','辛丑','壬寅','癸卯','甲辰','乙巳','丙午','丁未','戊申',
                           '己酉','庚戌','辛亥','壬子','癸丑','甲寅','乙卯','丙辰','丁巳','戊午','己未','庚申','辛酉','壬戌','癸亥');

        $lsRzNumL=array_search($sizhu[2],$lsjiazi);
        $lsRzNum=$lsRzNumL%15;$lsRzNum<=0?$lsRzNum=15:$lsRzNum;

        $dsJieqiGzSy=$lsjiazi[$lsRzNumL-$lsRzNum+1];
        $dsJieqiGSy=mb_substr($dsJieqiGzSy,0,1,'utf-8');

//        dump($jqDay);
//        dump($d);
//        dump($lsRzNum);
        //exit;

        //$runfa为1 拆补无闰法
        if($runfa!=1&&$jqDay>$d&&$lsRzNum<=8){//($jqDay<=$d-$lsRzNum&&)
            $jqkey=array_search($jieqi,$yonju24Arr);
            $jqkey+=1;$jqkey>23?$jqkey=0:$jqkey;
            $jieqi=$yonju24Arr[$jqkey];
        }elseif($jqDay==$d&&$lsRzNum==15&&$runfa!=1){
            $jqkey=array_search($jieqi,$yonju24Arr);
            $jqkey-=1;$jqkey<0?$jqkey=23:$jqkey;
            $jieqi=$yonju24Arr[$jqkey];
        }


        if($dsJieqiGSy=='己'){
            for($i=1;$i<=24;$i+=2){
                if($yonju24Arr[$i]==$jieqi){
                    $judgeJq=1;break;
                }
            }
            if($judgeJq!=1){
                //$jieqi=$yonju24Arr[$jqkey+1];
            }
        }

        $yonjuData=explode('-',$yonju24[$jieqi]);
        if($yonjuData[0]==1){$juDun='阳遁';}else{$juDun='阴遁';}

        $value=$lsRzNum;
        if($value<=5&&$value>=0){//上元
            $juNumKey=substr($yonjuData[1],0,1);
            $juDunName=$juDun.$numberName[$juNumKey].'局';$yuanName='上元';
        }elseif($value<=10&&$value>=6){//中元
            $juNumKey=substr($yonjuData[1],1,1);
            $juDunName=$juDun.$numberName[$juNumKey].'局';$yuanName='中元';
        }else{
            $juNumKey=substr($yonjuData[1],2,1);
            $juDunName=$juDun.$numberName[$juNumKey].'局';$yuanName='下元';
        }
        if($qijuType==1){//四柱起局
            $jushuYy=mb_substr($jushu,0,1,'utf-8');
            if($jushuYy=='阳'){$yonjuData[0]=1;}else{$yonjuData[0]=0;}

            $jushuDx=mb_substr($jushu,1,1,'utf-8');
            $juNumKey=$daXieNum[$jushuDx];
            $Arr['juDunName']=$jushu;
        }else{
            $Arr['juDunName']=$juDunName;
            $Arr['yuanName']="{$jieqi}:{$yuanName}";
        }

        //三奇六仪
        $gwNum=array('8'=>0,'3'=>1,'4'=>2,'9'=>3,'2'=>4,'7'=>5,'6'=>6,'1'=>7,'5'=>8);
        if($yonjuData[0]==1){//阳遁
            $sqlyArr='戊-己-庚-辛-壬-癸-丁-丙-乙';
        }else{//阴遁
            $sqlyArr='乙-丙-丁-癸-壬-辛-庚-己-戊';
        }
        $sqlyArr=explode('-',$sqlyArr);
        $dataNum=9-$juNumKey;
        if($yonjuData[0]==1){$dataNum+=1;}//阳遁

        $arrData=array_slice($sqlyArr,$dataNum);
        $sqlyArr=array_slice(array_merge($arrData,$sqlyArr),0,9);
        foreach($sqlyArr as $key=>$value){
            $Arr['sqly'][$gwNum[$key+1]]=$value;
            $sqlyGwArr[$key]=$value;
        }
        for($i=0;$i<=8;$i++){$sclyDataArr[$i]=$Arr['sqly'][$i];}

        if($qimen!=1) {//非飞盘奇门
            foreach($sclyDataArr as $key=>$value){//颜色处理
                if($value=='乙'||$value=='丙'||$value=='丁'){
                    $dataArrColour[$key]['text']=$value;
                    $dataArrColour[$key]['colour']='#404040';
                }else{
                    $dataArrColour[$key]['text']=$value;
                    $dataArrColour[$key]['colour']='#030303';
                }
            }
            $Arr['sqly']=$dataArrColour;
        }else{
            $Arr['sqly']=null;
        }

        //天盘奇仪
        $sgName=mb_substr($sizhu[3],0,1,'utf-8');
        if($sgName=='甲'){$sgName=$xsName;}
        $shiGNum=array_search($sgName,$sqlyArr);
        $xsNumber=array_search($xsName,$sqlyArr);
        $zhifuNumGw=$shiGNum;
        if($qimen!=1) {//非飞盘奇门
            if($shiGNum==4&&$sgName==$xsName){
                $tpSqlyArr=array_slice($sclyDataArr,0,8);
            }else{
                //$shiGNum==4?$shiGNum=1:$shiGNum;//宫位在5 借宫2
                $tpqxArr=array_slice($sclyDataArr,0,8);
                $tpxsNumber=array_search($xsName,$tpqxArr);
                $arrData=array_slice($tpqxArr,$tpxsNumber);
                $tpqxArr=array_slice(array_merge($arrData,$tpqxArr),0,8);
                $shiGNum=$gwNum[$shiGNum+1];
                $arrData=array_slice($tpqxArr,8-$shiGNum);
                $tpSqlyArr=array_slice(array_merge($arrData,$tpqxArr),0,8);
                if($xsNumber==4){//旬首宫位在5 借宫2
                    $arrData=array_slice($tpSqlyArr,4);
                    $tpSqlyArr=array_slice(array_merge($arrData,$tpSqlyArr),0,8);
                }
            }

            $Arr['tpsqly']=$tpSqlyArr;
        }
        //exit;

        //排八神
        if($qimen!=1) {//非飞盘奇门
            if($yonjuData[0]==1) {//阳遁
                $bashenArr=array('值符','腾蛇','太阴','六合','白虎','玄武','九地','九天',);
            }else{
                $bashenArr=array('值符','九天','九地','玄武','白虎','六合','太阴','腾蛇',);
            }
            $arrData=array_slice($bashenArr,8-$shiGNum);
            $bsArr=array_slice(array_merge($arrData,$bashenArr),0,8);

            foreach($bsArr as $key=>$value){//颜色处理
                if($value=='九天'||$value=='九地'||$value=='六合'||$value=='太阴'){
                    $bsArrColour[$key]['text']=$value;
                    $bsArrColour[$key]['colour']='#CD0000';
                }else{
                    $bsArrColour[$key]['text']=$value;
                    $bsArrColour[$key]['colour']='#030303';
                }
            }
            $Arr['bsArr']=$bsArrColour;
        }

        //排九星
        $yuanshiArr=array(
            array('zhishi'=>'休门','zhifu'=>'天蓬'),
            array('zhishi'=>'死门','zhifu'=>'天芮'),
            array('zhishi'=>'伤门','zhifu'=>'天冲'),
            array('zhishi'=>'杜门','zhifu'=>'天辅'),
            array('zhishi'=>'平门','zhifu'=>'天禽'),
            array('zhishi'=>'开门','zhifu'=>'天心'),
            array('zhishi'=>'惊门','zhifu'=>'天柱'),
            array('zhishi'=>'生门','zhifu'=>'天任'),
            array('zhishi'=>'景门','zhifu'=>'天英'),
        );
        $xsNumber=array_search($xsName,$sqlyArr);
        $zhishi=$yuanshiArr[$xsNumber]['zhishi'];
        $zhifu=$yuanshiArr[$xsNumber]['zhifu'];

        $Arr['zhifuName']="值符{$zhifu}落{$numberName[$zhifuNumGw+1]}宫";//落位
        if($zhifu=='天禽'){
            $zhifu='天芮';$zhishi='死门';
        }

        $buweiData=array();
        if($qimen!=1) {//非飞盘奇门
            $zhifuArr=array('天蓬','天任','天冲','天辅','天英','天芮','天柱','天心',);
            $zhifuNum=array_search($zhifu,$zhifuArr);
            $arrData=array_slice($zhifuArr,$zhifuNum);
            $zhifuArr=array_slice(array_merge($arrData,$zhifuArr),0,8);
            $arrData=array_slice($zhifuArr,8-$shiGNum);
            $zhifuArr=array_slice(array_merge($arrData,$zhifuArr),0,8);


            foreach($zhifuArr as $key=>$value){//颜色处理
                $buweiData[$key]='';
                if($value=='天任'||$value=='天冲'||$value=='天心'||$value=='天辅'){
                    $zhifuArrColour[$key]['text']=$value;
                    $zhifuArrColour[$key]['colour']='#CD0000';
                }elseif($value=='天芮'){
                    $zhifuArrColour[$key]['text']=$value;
                    $zhifuArrColour[$key]['colour']='#76EE00';
                    $buweiData[$key]=array("{$dataArrColour[8]['text']}",'禽');
                }else{
                    $zhifuArrColour[$key]['text']=$value;
                    $zhifuArrColour[$key]['colour']='#030303';
                }
            }
            $Arr['zhifuArr']=$zhifuArrColour;

            $Arr['buwei']=$buweiData;
        }

        //排八门
            $zhishiArr=array('休门','生门','伤门','杜门','景门','死门','惊门','开门',);
            $shiGSort=array_search(mb_substr($sizhu[3],0,1,'utf-8'),$tiangan);
            $shiGSort-=1;
            if($yonjuData[0]==1) {//阳遁
                $bamenNum=$xsNumber+$shiGSort;
                if($bamenNum>8){$bamenNum%=9;}
            }else{
                $bamenNum=$xsNumber-$shiGSort;
                if($bamenNum<0){$bamenNum+=9;}
            }

            $bamenNum+=1;
            $Arr['zhishiName']="值使{$zhishi}落{$numberName[$bamenNum]}宫";

        if($qimen!=1){//非飞盘奇门
            $zhishiNum=array_search($zhishi,$zhishiArr);
            $arrData=array_slice($zhishiArr,$zhishiNum);
            $zhishiArr=array_slice(array_merge($arrData,$zhishiArr),0,8);
            if($bamenNum==5){$bamenNum=2;}
            $arrData=array_slice($zhishiArr,8-$gwNum[$bamenNum]);
            $zhishiArr=array_slice(array_merge($arrData,$zhishiArr),0,8);
            foreach($zhishiArr as $key=>$value){//颜色处理
                if($value=='休门'||$value=='生门'||$value=='开门'){
                    $zhishiArrColour[$key]['text']=$value;
                    $zhishiArrColour[$key]['colour']='#3A5FCD';
                }else{
                    $zhishiArrColour[$key]['text']=$value;
                    $zhishiArrColour[$key]['colour']='#030303';
                }
            }
            $Arr['zhishiArr']=$zhishiArrColour;
        }


        //飞盘奇门排盘
        if($qimen==1){
            $Arr['qimen']=$qimen;
            $baguaArr=array('艮','震','巽','离','坤','兑','乾','坎','中',);
            $fpArr['bagua']=$baguaArr;
            //排九星
            $zhifuArr=array('天蓬','天芮','天冲','天辅','天禽','天心','天柱','天任','天英');
            $zhifuNum=array_search($zhifu,$zhifuArr);
            $arrData=array_slice($zhifuArr,$zhifuNum);
            $zhifuArr=array_slice(array_merge($arrData,$zhifuArr),0,9);
            $arrData=array_slice($zhifuArr,9-$shiGNum);
            $zhifuArr=array_slice(array_merge($arrData,$zhifuArr),0,9);

            foreach($zhifuArr as $key=>$value){
                $dataArr[$gwNum[$key+1]]=mb_substr($value,1,1,'utf-8');
            }
            $zhifuArr=$dataArr;
            ksort($zhifuArr);
            $fpArr['jiuxing']=$zhifuArr;

            //排八门
            $zhishiArr=array('休门','死门','伤门','杜门','中门','开门','惊门','生门','景门',);
            $zhishiNum=array_search($zhishi,$zhishiArr);
            $arrData=array_slice($zhishiArr,$zhishiNum);
            $zhishiArr=array_slice(array_merge($arrData,$zhishiArr),0,9);

            $arrData=array_slice($zhishiArr,10-$bamenNum);
            $zhishiArr=array_slice(array_merge($arrData,$zhishiArr),0,9);

            foreach($zhishiArr as $key=>$value){
                $dataArr[$gwNum[$key+1]]=mb_substr($value,0,1,'utf-8');
            }
            $zhishiArr=$dataArr;
            ksort($zhishiArr);
            $fpArr['bamen']=$zhishiArr;
            //排九神
            if($yonjuData[0]==1) {//阳遁
                $bashenArr=array('值符','腾蛇','太阴','六合','勾陈','太常','朱雀','九地','九天',);
            }else{
                $bashenArr=array('值符','九天','九地','朱雀','太常','勾陈','六合','太阴','腾蛇',);
            }
            $arrData=array_slice($bashenArr,9-$shiGNum);
            $jsArr=array_slice(array_merge($arrData,$bashenArr),0,9);
            foreach($jsArr as $key=>$value){
                $dataArr[$gwNum[$key+1]]=mb_substr($value,1,1,'utf-8');
            }
            $jsArr=$dataArr;
            ksort($jsArr);
            $fpArr['jiushen']=$jsArr;

            $fpArr['sqly']=$sclyDataArr;//三奇六仪
            //天盘三奇六仪
            if($yonjuData[0]==1){//阳遁
                $tpSqlyArr=array('辛','壬','癸','丁','丙','乙','戊','己','庚');
            }else{//阴遁
                $tpSqlyArr=array('癸','壬','辛','庚','己','戊','乙','丙','丁');
            }
            $dataNum=array_search($sgName,$sqlyGwArr);


            $tpxsNumber=array_search($xsName,$sqlyGwArr);
            $arrData=array_slice($sqlyGwArr,$tpxsNumber);
            $tpqxArr=array_slice(array_merge($arrData,$sqlyGwArr),0,9);
            //dump($tpqxArr);
            foreach($tpqxArr as $key=>$value){
                $tpqxArrData[$gwNum[$key+1]]=$value;
            }

            ksort($tpqxArrData);
            $fpArr['tpsqly']=$tpqxArrData;
            $Arr['fpData']=$fpArr;
        }
        $Arr['runfa']=$runfa;

        //dump($Arr);exit;
        return $Arr;
    }
    public function shiGzGet($sizhu,$tiangan,$dizhi){//时干支获取
        $sD=mb_substr($sizhu[3],0,1,'utf-8');
        $g=array_search(mb_substr($sizhu[2],0,1,'utf-8'),$tiangan);
        $sg=array_search($sD,$dizhi);
        $sg-=1;
        if($g==1||$g==6){
            $arrshizhu=array('甲','乙','丙','丁','戊','己','庚','辛','壬','癸','甲','乙');
        }else if($g==2||$g==7){
            $arrshizhu=array('丙','丁','戊','己','庚','辛','壬','癸','甲','乙','丙','丁');
        }else if($g==3||$g==8){
            $arrshizhu=array('戊','己','庚','辛','壬','癸','甲','乙','丙','丁','戊','己');
        }else if($g==4||$g==9){
            $arrshizhu=array('庚','辛','壬','癸','甲','乙','丙','丁','戊','己','庚','辛');
        }else{
            $arrshizhu=array('壬','癸','甲','乙','丙','丁','戊','己','庚','辛','壬','癸');
        }
        $sg=array_search($arrshizhu[$sg],$tiangan);
        $sg=$tiangan[$sg];
        return $sg.$sD;
    }
}